/*
 *	main-view view
 *  -----------------------------------------------------------------------------------------------------------
 */

site.view('create-account', {
	
	onLoad: function(settings) {
		this.startAll(settings);
	},
	
	onUnload: function() {
		this.stopAll();
	}

})

/*
 *	rain-animation
 *  -----------------------------------------------------------------------------------------------------------
 */

.module('wizzard', function(context) {
	
	var step = 1;

	return {
		init: function(){

		},

		events: {
			nextStep: function() {
				context.trigger('stepUnloaded-' + step++);
				context.trigger('stepLoaded-' + step);
			}
		}
	}
})

.module('step-1', function(context) {
	var util = context.util,
		require = context.require,
		tipsy = context.require('tipsy'),
		ajaxSubmitForm = require('ajaxSubmitForm'),
  		notify = require('notify'),
  		form;	

	return {
		init: function(){
			form  = context.query('#register_account_form');

			tipsy.init(form.find('input:text, input:password'), {
				gravity: 'w',
				trigger: 'focus'});

			ajaxSubmitForm.init('#register_account_form', {

				success: function(data) {
					notify.create("create", {
						title: data.message,
						text: data.description
					});  

					context.trigger('nextStep');
				},

				error: function(data) {
					var err_msg = '';

					util.eachRecursive(data.errors, function(key, val) {
						err_msg += err_msg === '' ? val : '<br/>' + val;
					})

					notify.create('create','error-template', {
						title: data.message,
						text: err_msg
					});
				}
			});
		},

		events: {
			'stepUnloaded-1': function() {
				form.parent('div').animate({'margin-top': '-376px'}, 500);
			}
		}
	}
})

.module('step-2', function(context) {
	var util = context.util,
		require = context.require,
		tipsy = context.require('tipsy'),
		multiselect = require('multiselect'),
		autocomplete = require('autocomplete'),
		datepicker = require('datepicker'),
		ajaxSubmitForm = require('ajaxSubmitForm'),		
  		notify = require('notify'),
		body,
		form,
		content;

	return {
		init: function(){
			body = context.query('body');
			form = context.query('#register_account_form_2');

			tipsy.init(form.find('input:text'), {
				gravity: 'w',
				trigger: 'focus'});
			multiselect.init('#field');

			autocomplete.init('#address', {
     			url: '/settings/get_locality'
   			 });

			datepicker.init(context.query('#birth_date'));

			context.query('#currently').on('change', function() {
				var experience_form = context.query('#experience_form'),
					education_form = context.query('#education_form'),
					else_form = context.query('#else_form');

				experience_form.hide();
				education_form.hide();
				else_form.hide();      
		      
	            switch ($(this).val()) {
	                case '0':
	                    experience_form.show();
	                    break;
	                case '1':
	                    education_form.show();
	                    break;
	                case '2':
	                    else_form.show();
	                    break;
	            }
			})

			ajaxSubmitForm.init('#register_account_form_2',{

				mapInputFields: {
						'field': 'standard_select_form0',
						'address': 'textboxlist'

					},

				success: function(data) {
					notify.create("create", {
						title: data.message,
						text: data.description
					});  

					setTimeout(function() {
						body.fadeOut();
						content = data.content;
						setTimeout(function(){			                  
							body.html(content).fadeIn();
							context.setView('main-login-view');			                        
						}, 500);
					}, 1500);

				},

				error: function(data) {
					var err_msg = '';

					util.eachRecursive(data.errors, function(key, val) {
						err_msg += err_msg === '' ? val : '<br/>' + val;
					})

					notify.create('create','error-template', {
						title: data.message,
						text: err_msg
					});
				}
			});
		},

		events: {
			'stepLoaded-2': function() {
				form.parent('div').parent('section').animate({'height':'470px'}, 500, function() {
                	$(this).css('height','auto');
             	});
			}
		}
	}
})